package com.collection.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class PublishConfig implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id; 
	
	private String userName;
	
	private String password;
	
	private String host;
	
	private String port;
	
	private String dbName;
	
	@Enumerated(EnumType.ORDINAL)
	private DB db;
	
	private String collectionDataCharCode;
	
	private String dbCharCode;
	
	@OneToMany(targetEntity = CollectionModelToPublishModel.class, cascade = { CascadeType.ALL })
    @JoinColumn(name="publishConfig_id",referencedColumnName="id")
	private List<CollectionModelToPublishModel> collectionModelToPublishModels;
	
	private String publishTableName;
	
	private String applicationRootPath;
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="collectionTask_id")  
	private CollectionTask collectionTask;
	
	public enum DB {
		MYSQL,
		SQLSERVER,
		SQLSERVER2005,	
		ORACLE
	}
	
	public PublishConfig() {
		
	}

	public PublishConfig(DB db, String host, String port, String dbName, String userName,
			String password, List<CollectionModelToPublishModel> collectionModelToPublishModels) {
		super();
		this.db = db;
		this.host = host;
		this.port = port;
		this.dbName = dbName;
		this.userName = userName;
		this.password = password;
		this.collectionModelToPublishModels = collectionModelToPublishModels;
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public Map<DB, String> getDriverClassMap() {
		Map<DB, String> driverClassMap = new HashMap<DB, String>();
		driverClassMap.put(DB.MYSQL, "com.mysql.jdbc.Driver");
		driverClassMap.put(DB.SQLSERVER, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
		driverClassMap.put(DB.SQLSERVER2005, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
		driverClassMap.put(DB.ORACLE, "oracle.jdbc.driver.OracleDriver");
		return driverClassMap;
	}

	public Map<DB, String> getDbURLMap() {
		Map<DB, String> dbURLMap = new HashMap<DB, String>();
		if(host != null && port != null && dbName != null) {
			dbURLMap.put(DB.MYSQL, "jdbc:mysql://"+host+":"+port+"/"+dbName);
			dbURLMap.put(DB.SQLSERVER, "jdbc:microsoft:sqlserver://"+host+":"+port+";databaseName="+dbName);
			dbURLMap.put(DB.SQLSERVER2005, "jdbc:sqlserver://"+host+":"+port+";databaseName="+dbName);
			dbURLMap.put(DB.ORACLE, "jdbc:oracle:thin:@"+host+":"+port+":"+dbName);
		}
		return dbURLMap;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public DB getDb() {
		return db;
	}

	public void setDb(DB db) {
		this.db = db;
	}
	
	public String getPublishTableName() {
		return publishTableName;
	}

	public void setPublishTableName(String publishTableName) {
		this.publishTableName = publishTableName;
	}

	public List<CollectionModelToPublishModel> getCollectionModelToPublishModels() {
		return collectionModelToPublishModels;
	}

	public void setCollectionModelToPublishModels(
			List<CollectionModelToPublishModel> collectionModelToPublishModels) {
		this.collectionModelToPublishModels = collectionModelToPublishModels;
	}

	public CollectionTask getCollectionTask() {
		return collectionTask;
	}

	public void setCollectionTask(CollectionTask collectionTask) {
		this.collectionTask = collectionTask;
	}

	public String getApplicationRootPath() {
		return applicationRootPath;
	}

	public void setApplicationRootPath(String applicationRootPath) {
		this.applicationRootPath = applicationRootPath;
	}

	public String getCollectionDataCharCode() {
		return collectionDataCharCode;
	}

	public void setCollectionDataCharCode(String collectionDataCharCode) {
		this.collectionDataCharCode = collectionDataCharCode;
	}

	public String getDbCharCode() {
		return dbCharCode;
	}

	public void setDbCharCode(String dbCharCode) {
		this.dbCharCode = dbCharCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj != null && (obj instanceof PublishConfig)) {
			return ((PublishConfig) obj).getId().equals(id);
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		if (getId() == null) {
			return super.hashCode();
		} else {
			return this.getId().hashCode();
		}
	}
}
